layui.use(['table','tfApi'], function () {
    var $ = layui.jquery, table = layui.table, API = layui.tfApi;


    table.render({
        elem: '#applyTable',
        url: API.CURL + API.invoice.qryInvoice, // '/seller/qrySuccInvoice',
        method: "POST",
        contentType: 'application/json',
        xhrFields: {withCredentials: true},crossDomain: true,
        request:{
            pageName: "page",
            limitName : "pageSize"
        },
        parseData: function(res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.details, //解析数据列表
            };
        },
        cols: [[
            {field: 'id',title: '订单ID', hide: true},
            {field: 'sellerAccountId',title: '商户ID', hide: true},
            {field: 'serviceId',title: '服务商ID', hide: true},
            {field: 'sellerName', title: '购票公司'},
            {field: 'provideAmount', title: '业务费金额',templet: function (d) {
                    //var _s = API.numFun(d.realAmount);//d.investAmount - d.serviceAmount;
                    //return '<b>'+_s+'</b>';
                    var _sp = d.investAmount*100,
                        _bi = d.serviceAmount*100,
                        _ss = _sp - _bi,
                        _s = (_ss == 0) ? _ss : (_ss/100),
                        _sr = API.numFun(_s);
                    return '<b>'+_sr+'</b>';
                }},
            {field: 'serviceAmount', title: '服务费金额',templet: function (d) {
                    var _s = API.numFun(d.serviceAmount);
                    return '<b>'+_s+'</b>';
                }},
            {field: 'syAmount', title: '剩余开票金额',templet: function (d) {
                    var _sp = d.investAmount*100,
                        _bi = d.billTotal*100,
                        _ss = _sp - _bi,
                        _s = (_ss == 0) ? _ss : (_ss/100),
                        _sr = API.numFun(_s);
                    return '<b>'+_sr+'</b>';
                }},
            {field: 'serviceName', title: '开票公司'},
            {field: 'orderType', title: '状态', toolbar: '#barBtn' , align: 'center'}
        ]],
        page: true,
        height : 'auto'
    });


    table.on('tool(applyFilter)', function (obj) {
        var _data = obj.data,_id = _data.serviceId;

        var _sp = _data.investAmount*100,
            _bi = _data.billTotal*100,
            _ss = _sp - _bi,
            _s = (_ss == 0) ? _ss : (_ss/100),
            _amount = API.numFun(_s);


        console.log(_data);

        if (obj.event === 'apply') {  // 监听申请发票操作
            var index = layer.open({
                title: '申请发票',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '../../page/invoice/applyInfo.html?'+_id+"&"+_amount+"&"+_data.sellerAccountId,
                success: function (layero, index) {
                    var frameId = $(layero).find('iframe').attr("id");
                    //找到子页面的document对象
                    var document = window.frames[frameId].document;
                    //根据id找到子页面中相应的元素并赋值
                    var _sp = _data.provideAmount*100,
                        _bi = _data.billTotal*100,
                        _ss = _sp - _bi,
                        __syAmount = (_ss == 0) ? _ss : (_ss/100);
                    $(document).find("input[name='sellerId']").val(_data.sellerAccountId);
                    $(document).find("input[name='serviceId']").val(_data.serviceId);
                    $(document).find("input[name='orderId']").val(_data.id);
                    $(document).find("input[name='orderSource']").val('1');
                    $(document).find("#invoiceAmount").attr('data-amount',__syAmount);
                },
                end:function(){
                    location.reload();
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        }
    });

});

